<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 商品分类卡片 -->

    <el-card class="box-card">
      <el-button type="primary" @click="addCateInfo" class="addCate"
        >添加分类</el-button
      >
      <!-- 表格 -->
      <template>
        <!-- <el-table :data="CateList" border style="width: 100%">
          <el-table-column prop="cat_name" label="分类名称" width="180">
          </el-table-column>
          <el-table-column label="是否有效" width="180">
            <template slot-scope="scope">
              <i class="el-icon-circle-check" v-if="scope.row.cat_deleted"></i>
              <i class="el-icon-circle-close" v-else></i>
            </template>
          </el-table-column>
          <el-table-column prop="cat_level" label="排序"> </el-table-column>
        </el-table> -->
        <win-tree
          :data="CateList"
          :columns="tableConfig"
          :expand-type="false"
          :selection-type="false"
          :show-index="true"
          :border="true"
        >
          <template slot="isflag" slot-scope="scope">
            <i
              class="el-icon-success"
              style="color:green"
              v-if="scope.row.cat_deleted"
            ></i>
            <i class="el-icon-error" style="color:red" v-else></i>
          </template>

          <template slot="order" slot-scope="scope">
            <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag type="success" v-else-if="scope.row.cat_level === 1"
              >二级</el-tag
            >
            <el-tag type="warning" v-else>三级</el-tag>
          </template>

          <template slot="operation" slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="editCate(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="delCate(scope.row)"
              >删除</el-button
            >
          </template>
        </win-tree>
      </template>

      <!-- 修改分类信息 -->
      <el-dialog
        title="修改分类信息"
        :visible.sync="editCateVisible"
        width="30%"
      >
        <el-form label-position="left" label-width="80px" :model="editCateForm">
          <el-form-item label="分类名称">
            <el-input v-model="editCateForm.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editCateVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCateInfo">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 增加分类 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addCateVisible"
        width="30%"
        @close="deladdCate"
      >
        <el-form
          label-position="left"
          label-width="80px"
          :model="addCateForm"
          ref="addCateForm"
        >
          <el-form-item label="分类名称">
            <el-input v-model="addCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="分类级别">
            <el-cascader
              ref="cascaderRef"
              v-model="selectKey"
              :options="options"
              :props="{
                checkStrictly: true,
                expandTrigger: 'hover',
                label: 'cat_name',
                value: 'cat_id',
                children: 'children',
              }"
              @change="handleChange"
              :show-all-levels="false"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateVisible = false">取 消</el-button>
          <el-button type="primary" @click="addNewCate"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.pagenum"
        :page-sizes="[5, 6, 8, 10]"
        :page-size="pagination.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      CateList: [],
      editCateForm: {
        id: "",
        cat_name: "",
      },
      addCateVisible: false,
      options: [],
      addCateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      editCateVisible: false,
      selectKey: [],
      pagination: {
        total: 10,
        pagenum: 1,
        pagesize: 10,
      },
      tableConfig: [
        {
          label: "分类名称",
          prop: "cat_name",
          width: "200px",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isflag",
          width: "200px",
        },
        {
          label: "排序",
          template: "order",
          type: "template",
          width: "200px",
        },
        {
          label: "操作",
          template: "operation",
          type: "template",
          width: "200px",
        },
      ],
    };
  },
  mounted() {
    this.getoptions();
    this.getCateList();
    setInterval(function() {
      document.querySelectorAll(".el-cascader-node__label").forEach((el) => {
        el.onclick = function() {
          if (this.previousElementSibling) this.previousElementSibling.click();
        };
      });
    }, 1000);
  },
  watch: {
    selectKey() {
      console.log("执行");
      this.$refs.cascaderRef.dropDownVisible = false;
    },
  },
  methods: {
    //重置添加分类表单
    deladdCate() {
      // this.$refs.addCateForm.resetFields();
      this.addCateForm.cat_name = "";
      this.selectKey = [];
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    },

    // 获取级联选择框数据
    async getoptions() {
      const { data: res } = await this.$axios.get("categories",{
        params:{
          type:2
        }
      });
      // console.log(res,"吉安");
      this.options = res.data;
      console.log(res.data);
    },

    // 改变级联选择框的值
    handleChange(val) {
      if (this.selectKey.length > 0) {
        this.addCateForm.cat_pid = this.selectKey[this.selectKey.length - 1];
        this.addCateForm.cat_level = this.selectKey.length;
      }
      console.log(this.addCateForm.cat_pid);
      // console.log(this.selectKey);
      // console.log(this.addCateForm.cat_pid);
    },

    // 获取商品分类列表数据
    async getCateList() {
      const { data: res } = await this.$axios.get("categories", {
        params: {
          pagenum: this.pagination.pagenum,
          pagesize: this.pagination.pagesize,
        },
      });
      this.pagination.total = res.data.total;
      this.CateList = res.data.result;
      // console.log(res.data.result);
    },

    //点击添加分类
    addCateInfo() {
      this.addCateVisible = true;
      // console.log(this.$refs.addCateForm);
    },

    // 点击修改分类
    editCate(row) {
      // console.log(row.cat_id);
      this.editCateForm.id = row.cat_id;
      this.editCateVisible = true;
    },

    //点击确定增加分类
    async addNewCate(){
      this.addCateVisible = false;
      const {data:res} = await this.$axios.post('categories',{
        cat_pid:this.addCateForm.cat_pid,
        cat_level:this.selectKey.legnth,
        cat_name:this.addCateForm.cat_name
      })

      if(res.meta.status == 201){
        this.getCateList()
        this.$message.success("添加分类成功")
      }
    },

    //点击确定修改分类
    async editCateInfo() {
      const { data: res } = await this.$axios.put(
        "categories/" + this.editCateForm.id,
        {
          cat_name: this.editCateForm.cat_name,
        }
      );
      if (res.meta.status == 200) {
        this.$message.success("修改分类信息成功");
        this.getCateList();
      }
      this.editCateVisible = false;
    },

    // 页码改变触发
    handleSizeChange(newpagesize) {
      this.pagination.pagenum = 1;
      this.pagination.pagesize = newpagesize;
      this.getCateList();
    },

    // 页数改变触发
    handleCurrentChange(newpagenum) {
      this.pagination.pagenum = newpagenum;
      this.getCateList();
    },

    // 点击删除分类
    async delCate(row) {
      const flag = await this.$confirm(
        "此操作将永久删除该分类, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      if (flag == "confirm") {
        const { data: res } = await this.$axios.delete(
          "categories/" + row.cat_id
        );
        if (res.meta.status == 200) {
          this.$message.success("删除分类成功");
          this.getCateList();
        }
      }
    },
  },
};
</script>

<style scope>
.addCate {
  margin-bottom: 20px;
}

.el-radio {
  visibility: hidden;

}
.el-cascader__dropdown{
  animation: all 1s linear;
}
</style>
<style module></style>
